package com.tkjt.pms.common.utils;

import com.tkjt.pms.common.vos.RequestJson;
import net.sf.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class EsJsonUtils {

	public static Map<String, Object> bean2Map(Object object) {
		Map<String, Object> data = new HashMap<String, Object>();
		JSONObject jsonObject = JSONObject.fromObject(object);
		Iterator<?> it = jsonObject.keys();
		while (it.hasNext()) {
			String key = String.valueOf(it.next());
			Object value = jsonObject.get(key);
			if (key.charAt(0) >= 'A' && key.charAt(0) <= 'Z') {
				String firstChar = "" + key.charAt(0);
				firstChar = firstChar.toLowerCase();
				key = firstChar + key.substring(1);
			}
			data.put(key, value);
		}
		return data;
	}
	
	public static Object json2Object(String json, Class<?> beanClz) {
		return JSONObject.toBean(JSONObject.fromObject(json), beanClz);
	}
	
	public static Object map2Bean(Map<String, Object> map, Class<?> beanClz) {
		JSONObject jsonObject = JSONObject.fromObject(map);
		return JSONObject.toBean(jsonObject, beanClz);
	}
	
	public static String toJsonStr(Object object) {
		JSONObject jsonObject = JSONObject.fromObject(object);
		return jsonObject.toString();
	}


    /**
     * 获取HttpServletRequest入参
     * @return
     * @throws IOException
     */
    public static RequestJson getRequestJsonObject(HttpServletRequest request) {
        try {
            String json = getRequestJsonString(request);
            return new RequestJson(JSONObject.fromObject(json));
        }catch (Exception e){
            return new RequestJson();
        }
    }
    /***
     * 获取 request 中 json 字符串的内容
     *
     * @param request
     * @return : <code>byte[]</code>
     * @throws IOException
     */
    public static String getRequestJsonString(HttpServletRequest request)
            throws IOException {
        String submitMehtod = request.getMethod();
        // GET
        if (submitMehtod.equals("GET")) {
            return new String(request.getQueryString().getBytes("iso-8859-1"),"utf-8").replaceAll("%22", "\"");
            // POST
        } else {
            int contentLength = request.getContentLength();
            if(contentLength<0){
                return null;
            }
            byte buffer[] = new byte[contentLength];
            for (int i = 0; i < contentLength;) {

                int readlen = request.getInputStream().read(buffer, i,
                        contentLength - i);
                if (readlen == -1) {
                    break;
                }
                i += readlen;
            }
            String charEncoding = request.getCharacterEncoding();
            if (charEncoding == null) {
                charEncoding = "UTF-8";
            }
            return new String(buffer, charEncoding);
        }
    }


}
